/*------------------------------------------------------------------------*/
/*  Copyright 2014 National Renewable Energy Laboratory.                  */
/*  This software is released under the license detailed                  */
/*  in the file, LICENSE, which is located in the top-level Nalu          */
/*  directory structure                                                   */
/*------------------------------------------------------------------------*/

#include "kernels/UnitTestKernelUtils.h"
#include "UnitTestUtils.h"
#include "UnitTestHelperObjects.h"

#include "kernel/MomentumHybridTurbElemKernel.h"

namespace {
namespace hex8_golds {
namespace MomentumHybridTurbElemKernel {
static constexpr double rhs[24] = {
  0.0020843046014210665,  -0.0020843046014210687, 0.0000000000000000,
  -0.002084304601421067,  -0.014590132209947463,  0.0000000000000000,
  -0.014590132209947463,  0.014590132209947464,   0.0000000000000000,
  0.014590132209947461,   0.002084304601421067,   0.0000000000000000,
  0.0020843046014210661,  -0.0020843046014210648, 0.0000000000000000,
  -0.0020843046014210661, -0.014590132209947461,  0.0000000000000000,
  -0.014590132209947466,  0.014590132209947468,   0.0000000000000000,
  0.014590132209947464,   0.0020843046014210631,  0.0000000000000000,
};

static constexpr double lhs[24][24] = {
  {
    0.0562500000000000,  0.0093750000000000,  0.0093750000000000,
    -0.018750000000000,  -0.0093750000000000, -0.0093750000000000,
    -0.0125000000000000, -0.0093750000000000, -0.0031250000000000,
    0.0000000000000000,  0.0093750000000000,  0.0031250000000000,
    0.0000000000000000,  0.0031250000000000,  0.0093750000000000,
    -0.0125000000000000, -0.0031250000000000, -0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, -0.0031250000000000,
    -0.0062500000000000, 0.0031250000000000,  0.0031250000000000,
  },
  {
    0.0093750000000000,  0.0562500000000000,  0.0093750000000000,
    0.0093750000000000,  0.0000000000000000,  0.0031250000000000,
    -0.0093750000000000, -0.0125000000000000, -0.0031250000000000,
    -0.0093750000000000, -0.018750000000000,  -0.0093750000000000,
    0.0031250000000000,  0.0000000000000000,  0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0125000000000000, -0.0093750000000000,
  },
  {
    0.0093750000000000,  0.0093750000000000,  0.0562500000000000,
    0.0093750000000000,  0.0031250000000000,  0.0000000000000000,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  0.0000000000000000,
    -0.0093750000000000, -0.0093750000000000, -0.0187500000000000,
    -0.0093750000000000, -0.0031250000000000, -0.0125000000000000,
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, -0.0125000000000000,
  },
  {
    -0.018750000000000,   0.0093750000000000,  0.0093750000000000,
    0.056250000000000000, -0.0093750000000000, -0.0093750000000000,
    0.0000000000000000,   -0.0093750000000000, -0.0031250000000000,
    -0.0125000000000000,  0.0093750000000000,  0.0031250000000000,
    -0.0125000000000000,  0.0031250000000000,  0.0093750000000000,
    0.0000000000000000,   -0.0031250000000000, -0.0093750000000000,
    -0.0062500000000000,  -0.0031250000000000, -0.0031250000000000,
    -0.0062500000000000,  0.0031250000000000,  0.0031250000000000,
  },
  {
    -0.0093750000000000, 0.0000000000000000,   0.0031250000000000,
    -0.0093750000000000, 0.056250000000000000, 0.0093750000000000,
    0.0093750000000000,  -0.0187500000000000,  -0.0093750000000000,
    0.0093750000000000,  -0.0125000000000000,  -0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000,  0.0031250000000000,
    -0.0031250000000000, 0.0000000000000000,   0.0093750000000000,
    0.0031250000000000,  -0.0125000000000000,  -0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000,  -0.0031250000000000,
  },
  {
    -0.0093750000000000, 0.0031250000000000,  0.0000000000000000,
    -0.0093750000000000, 0.0093750000000000,  0.056250000000000000,
    -0.0031250000000000, 0.0093750000000000,  0.0000000000000000,
    -0.0031250000000000, 0.0031250000000000,  -0.0062500000000000,
    0.0093750000000000,  -0.0031250000000000, -0.0125000000000000,
    0.0093750000000000,  -0.0093750000000000, -0.018750000000000,
    0.0031250000000000,  -0.0093750000000000, -0.0125000000000000,
    0.0031250000000000,  -0.0031250000000000, -0.0062500000000000,
  },
  {
    -0.0125000000000000, -0.0093750000000000, 0.0031250000000000,
    -0.0000000000000000, 0.0093750000000000,  -0.0031250000000000,
    0.0562500000000000,  0.0093750000000000,  -0.0093750000000000,
    -0.018750000000000,  -0.0093750000000000, 0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, 0.0031250000000000,
    -0.0062500000000000, 0.0031250000000000,  -0.0031250000000000,
    0.0000000000000000,  0.0031250000000000,  -0.0093750000000000,
    -0.0125000000000000, -0.0031250000000000, 0.0093750000000000,
  },
  {
    -0.0093750000000000, -0.0125000000000000, 0.0031250000000000,
    -0.0093750000000000, -0.0187500000000000, 0.0093750000000000,
    0.0093750000000000,  0.0562500000000000,  -0.0093750000000000,
    0.0093750000000000,  0.0000000000000000,  -0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0125000000000000, 0.0093750000000000,
    0.0031250000000000,  0.0000000000000000,  -0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000, -0.0031250000000000,
  },
  {
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, -0.0000000000000000,
    -0.0093750000000000, -0.0093750000000000, 0.0562500000000000,
    -0.0093750000000000, -0.0031250000000000, 0.0000000000000000,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  -0.0125000000000000,
    0.0093750000000000,  0.0093750000000000,  -0.0187500000000000,
    0.0093750000000000,  0.0031250000000000,  -0.0125000000000000,
  },
  {
    -0.0000000000000000, -0.0093750000000000, 0.0031250000000000,
    -0.0125000000000000, 0.0093750000000000,  -0.0031250000000000,
    -0.0187500000000000, 0.0093750000000000,  -0.0093750000000000,
    0.0562500000000000,  -0.0093750000000000, 0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, 0.0031250000000000,
    -0.0062500000000000, 0.0031250000000000,  -0.0031250000000000,
    -0.0125000000000000, 0.0031250000000000,  -0.0093750000000000,
    -0.0000000000000000, -0.0031250000000000, 0.0093750000000000,
  },
  {
    0.0093750000000000,  -0.0187500000000000, 0.0093750000000000,
    0.0093750000000000,  -0.0125000000000000, 0.0031250000000000,
    -0.0093750000000000, -0.0000000000000000, -0.0031250000000000,
    -0.0093750000000000, 0.0562500000000000,  -0.0093750000000000,
    0.0031250000000000,  -0.0125000000000000, 0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0000000000000000, -0.0093750000000000,
  },
  {
    0.0031250000000000,  -0.0093750000000000, -0.0000000000000000,
    0.0031250000000000,  -0.0031250000000000, -0.0062500000000000,
    0.0093750000000000,  -0.0031250000000000, -0.0000000000000000,
    0.0093750000000000,  -0.0093750000000000, 0.0562500000000000,
    -0.0031250000000000, 0.0093750000000000,  -0.0125000000000000,
    -0.0031250000000000, 0.0031250000000000,  -0.0062500000000000,
    -0.0093750000000000, 0.0031250000000000,  -0.0125000000000000,
    -0.0093750000000000, 0.0093750000000000,  -0.018750000000000,
  },
  {
    0.0000000000000000,  0.0031250000000000,  -0.0093750000000000,
    -0.0125000000000000, -0.0031250000000000, 0.0093750000000000,
    -0.0062500000000000, -0.0031250000000000, 0.0031250000000000,
    -0.0062500000000000, 0.0031250000000000,  -0.0031250000000000,
    0.0562500000000000,  0.0093750000000000,  -0.0093750000000000,
    -0.0187500000000000, -0.0093750000000000, 0.0093750000000000,
    -0.0125000000000000, -0.0093750000000000, 0.0031250000000000,
    0.0000000000000000,  0.0093750000000000,  -0.0031250000000000,
  },
  {
    0.0031250000000000,  0.0000000000000000,  -0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0125000000000000, 0.0093750000000000,
    0.0093750000000000,  0.0562500000000000,  -0.0093750000000000,
    0.0093750000000000,  -0.0000000000000000, -0.0031250000000000,
    -0.0093750000000000, -0.0125000000000000, 0.0031250000000000,
    -0.0093750000000000, -0.0187500000000000, 0.0093750000000000,
  },
  {
    0.0093750000000000,  0.0093750000000000,  -0.0187500000000000,
    0.0093750000000000,  0.0031250000000000,  -0.0125000000000000,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  -0.0125000000000000,
    -0.0093750000000000, -0.0093750000000000, 0.0562500000000000,
    -0.0093750000000000, -0.0031250000000000, -0.0000000000000000,
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, 0.0000000000000000,
  },
  {
    -0.0125000000000000,  0.0031250000000000,  -0.0093750000000000,
    0.0000000000000000,   -0.0031250000000000, 0.0093750000000000,
    -0.0062500000000000,  -0.0031250000000000, 0.0031250000000000,
    -0.0062500000000000,  0.0031250000000000,  -0.0031250000000000,
    -0.018750000000000,   0.0093750000000000,  -0.0093750000000000,
    0.056250000000000000, -0.0093750000000000, 0.0093750000000000,
    0.0000000000000000,   -0.0093750000000000, 0.0031250000000000,
    -0.0125000000000000,  0.0093750000000000,  -0.0031250000000000,
  },
  {
    -0.0031250000000000, -0.0062500000000000,  -0.0031250000000000,
    -0.0031250000000000, 0.0000000000000000,   -0.0093750000000000,
    0.0031250000000000,  -0.0125000000000000,  0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000,  0.0031250000000000,
    -0.0093750000000000, 0.0000000000000000,   -0.0031250000000000,
    -0.0093750000000000, 0.056250000000000000, -0.0093750000000000,
    0.0093750000000000,  -0.0187500000000000,  0.0093750000000000,
    0.0093750000000000,  -0.0125000000000000,  0.0031250000000000,
  },
  {
    -0.0093750000000000, 0.0031250000000000,  -0.0125000000000000,
    -0.0093750000000000, 0.0093750000000000,  -0.018750000000000,
    -0.0031250000000000, 0.0093750000000000,  -0.0125000000000000,
    -0.0031250000000000, 0.0031250000000000,  -0.0062500000000000,
    0.0093750000000000,  -0.0031250000000000, 0.0000000000000000,
    0.0093750000000000,  -0.0093750000000000, 0.056250000000000000,
    0.0031250000000000,  -0.0093750000000000, 0.0000000000000000,
    0.0031250000000000,  -0.0031250000000000, -0.0062500000000000,
  },
  {
    -0.0062500000000000, -0.0031250000000000, -0.0031250000000000,
    -0.0062500000000000, 0.0031250000000000,  0.0031250000000000,
    0.0000000000000000,  0.0031250000000000,  0.0093750000000000,
    -0.0125000000000000, -0.0031250000000000, -0.0093750000000000,
    -0.0125000000000000, -0.0093750000000000, -0.0031250000000000,
    -0.0000000000000000, 0.0093750000000000,  0.0031250000000000,
    0.0562500000000000,  0.0093750000000000,  0.0093750000000000,
    -0.018750000000000,  -0.0093750000000000, -0.0093750000000000,
  },
  {
    -0.0031250000000000, -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0125000000000000, -0.0093750000000000,
    0.0031250000000000,  0.0000000000000000,  0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000, 0.0031250000000000,
    -0.0093750000000000, -0.0125000000000000, -0.0031250000000000,
    -0.0093750000000000, -0.0187500000000000, -0.0093750000000000,
    0.0093750000000000,  0.0562500000000000,  0.0093750000000000,
    0.0093750000000000,  0.0000000000000000,  0.0031250000000000,
  },
  {
    -0.0031250000000000, -0.0031250000000000, -0.0062500000000000,
    -0.0031250000000000, -0.0093750000000000, -0.0125000000000000,
    -0.0093750000000000, -0.0093750000000000, -0.0187500000000000,
    -0.0093750000000000, -0.0031250000000000, -0.0125000000000000,
    0.0031250000000000,  0.0031250000000000,  -0.0062500000000000,
    0.0031250000000000,  0.0093750000000000,  -0.0000000000000000,
    0.0093750000000000,  0.0093750000000000,  0.0562500000000000,
    0.0093750000000000,  0.0031250000000000,  0.0000000000000000,
  },
  {
    -0.0062500000000000, -0.0031250000000000, -0.0031250000000000,
    -0.0062500000000000, 0.0031250000000000,  0.0031250000000000,
    -0.0125000000000000, 0.0031250000000000,  0.0093750000000000,
    -0.0000000000000000, -0.0031250000000000, -0.0093750000000000,
    -0.0000000000000000, -0.0093750000000000, -0.0031250000000000,
    -0.0125000000000000, 0.0093750000000000,  0.0031250000000000,
    -0.0187500000000000, 0.0093750000000000,  0.0093750000000000,
    0.0562500000000000,  -0.0093750000000000, -0.0093750000000000,
  },
  {
    0.0031250000000000,  -0.0125000000000000, -0.0093750000000000,
    0.0031250000000000,  -0.0062500000000000, -0.0031250000000000,
    -0.0031250000000000, -0.0062500000000000, 0.0031250000000000,
    -0.0031250000000000, -0.0000000000000000, 0.0093750000000000,
    0.0093750000000000,  -0.0187500000000000, -0.0093750000000000,
    0.0093750000000000,  -0.0125000000000000, -0.0031250000000000,
    -0.0093750000000000, -0.0000000000000000, 0.0031250000000000,
    -0.0093750000000000, 0.0562500000000000,  0.0093750000000000,
  },
  {
    0.0031250000000000,  -0.0093750000000000, -0.0125000000000000,
    0.0031250000000000,  -0.0031250000000000, -0.0062500000000000,
    0.0093750000000000,  -0.0031250000000000, -0.0125000000000000,
    0.0093750000000000,  -0.0093750000000000, -0.018750000000000,
    -0.0031250000000000, 0.0093750000000000,  -0.0000000000000000,
    -0.0031250000000000, 0.0031250000000000,  -0.0062500000000000,
    -0.0093750000000000, 0.0031250000000000,  -0.0000000000000000,
    -0.0093750000000000, 0.0093750000000000,  0.0562500000000000,
  },
};
} // namespace MomentumHybridTurbElemKernel

} // namespace hex8_golds
} // anonymous namespace

TEST_F(HybridTurbKernelHex8Mesh, MomentumHybridTurbElemKernel)
{
  fill_mesh_and_init_fields();

  // Setup solution options for default advection kernel
  solnOpts_.meshMotion_ = false;
  solnOpts_.meshDeformation_ = false;
  solnOpts_.externalMeshDeformation_ = false;
  solnOpts_.initialize_turbulence_constants();

  unit_test_utils::HelperObjects helperObjs(
    bulk_, stk::topology::HEX_8, 3, partVec_[0]);

  // Initialize the kernel
  std::unique_ptr<sierra::nalu::Kernel> kernel(
    new sierra::nalu::MomentumHybridTurbElemKernel<sierra::nalu::AlgTraitsHex8>(
      bulk_, solnOpts_, velocity_,
      helperObjs.assembleElemSolverAlg->dataNeededByKernels_));

  // Add to kernels to be tested
  helperObjs.assembleElemSolverAlg->activeKernels_.push_back(kernel.get());

  // Populate LHS and RHS
  helperObjs.assembleElemSolverAlg->execute();

  EXPECT_EQ(helperObjs.linsys->lhs_.extent(0), 24u);
  EXPECT_EQ(helperObjs.linsys->lhs_.extent(1), 24u);
  EXPECT_EQ(helperObjs.linsys->rhs_.extent(0), 24u);

  namespace gold_values = ::hex8_golds::MomentumHybridTurbElemKernel;
  unit_test_kernel_utils::expect_all_near(
    helperObjs.linsys->rhs_, gold_values::rhs);
  unit_test_kernel_utils::expect_all_near<24>(
    helperObjs.linsys->lhs_, gold_values::lhs);
}
